// 使用消息传递在线程间传送数据
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

// 信道与所有权转移
// fn main() {
//     let (tx, rx) = mpsc::channel();
//     thread::spawn(move || {
//         let val = String::from("h1");
//         tx.send(val).unwrap();
//         // val的所有权已经被转移，下面的语句出错
//         // println!("val is {}",val);
//     });
//     let received = rx.recv().unwrap();
//     println!("Got: {}", received);
// }

// 发送多个值并观察接收者的等待
// fn main() {
//     let (tx, rx) = mpsc::channel();
//     thread::spawn(move || {
//         let vals = vec![
//             String::from("hi"),
//             String::from("from"),
//             String::from("the"),
//             String::from("thread"),
//         ];
//         for val in vals {
//             tx.send(val).unwrap();
//             thread::sleep(Duration::from_secs(1));
//         }
//     });
//     for received in rx {
//         println!("Got: {}", received);
//     }
// }

// 通过克隆发送者来创建多个生产者
fn main() {
    let (tx, rx) = mpsc::channel();
    let tx1 = tx.clone();
    thread::spawn(move || {
        let vals = vec![
            String::from("hi"),
            String::from("from"),
            String::from("the"),
            String::from("thread"),
        ];
        for val in vals {
            tx1.send(val).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });
    thread::spawn(move || {
        let vals = vec![
            String::from("more"),
            String::from("messages"),
            String::from("for"),
            String::from("you"),
        ];
        for val in vals {
            tx.send(val).unwrap();
            thread::sleep(Duration::from_secs(1));
        }
    });
    for received in rx {
        println!("Got: {}", received);
    }
}
